package output;

import instance.Instance;
import instance.InstanceEj2;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import problem.ej2.Valla;

public class Problem2Output implements Output {

	private static String outputFile;
	private List<Integer> solutions;

	public Problem2Output(List<Integer> solutions) {
		this.solutions = solutions;
	}

	public Problem2Output(String nombArch) {

		outputFile = nombArch;
	}

	public void writeInstance(LinkedList<Instance> lista_insts)
			throws Exception {
		/*
		 * ||CUIDADO||:Usar después de generar el tipo Problem2Output con el
		 * constructor que cambia el outputfile
		 */
		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));

		Iterator<Instance> it = lista_insts.iterator();

		while (it.hasNext()) {
			InstanceEj2 inst = (InstanceEj2) it.next();

			String langosta = inst.getSalto_Langostas() + "\n";
			bw.write(langosta);

			Iterator<Valla> it2 = inst.getVallas().iterator();

			while (it2.hasNext()) {
				Valla v = it2.next();
				String datos = v.getX() + " " + v.getY() + " " + v.getD() + " "
						+ v.getW() + " " + v.getH();
				bw.write(datos);
				if (it2.hasNext())
					bw.write(";");
			}
			bw.newLine();
		}

		bw.close();
	}

	public void writeTestRend(LinkedList<String> strings_salida)
			throws Exception {
		/*
		 * ||CUIDADO||:Usar después de generar el tipo Problem2Output con el
		 * constructor que cambia el outputfile
		 */
		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));

		Iterator<String> it_res = strings_salida.iterator();
		while (it_res.hasNext()) {
			String st = it_res.next();
			bw.write(st);
			if (it_res.hasNext())
				bw.newLine();
		}
		bw.close();
	}

	@Override
	public void writeFile(String soluciones) throws Exception {

		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));

		bw.write(soluciones);
		bw.newLine();
		bw.close();
	}

	public void escribirSolucion(String outputFile) throws Exception {

		this.outputFile = outputFile;

		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));

		Iterator<Integer> it_res = solutions.iterator();
		while (it_res.hasNext()) {
			String st = it_res.next().toString();
			bw.write(st);
			if (it_res.hasNext())
				bw.newLine();
		}
		bw.flush();
		bw.close();
	}
}
